package gtug.duesseldorf.client.ui;

import gtug.duesseldorf.client.Wave_webclient;
import gtug.duesseldorf.client.model.ClientCommand;
import gtug.duesseldorf.client.model.ClientParticipant;
import gtug.duesseldorf.client.model.ClientWaveId;

import java.util.List;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

public class WaveContent extends Composite {

	private final HeaderPanel m_headerPanel;

	private final WaveTextArea m_textArea;

	private ClientWaveId m_openWaveId;

	public WaveContent(Wave_webclient client) {

		FlexTable t = new FlexTable();
		t.setSize("100%", "100%");
		initWidget(t);

		FlexCellFormatter f = t.getFlexCellFormatter();

		m_headerPanel = new HeaderPanel(client);
		t.setWidget(0, 0, m_headerPanel);
		f.setHeight(0, 0, "10%");

		m_textArea = new WaveTextArea();
		t.setWidget(1, 0, m_textArea);
		f.setHeight(1, 0, "90%");
	}

	public void clear() {
		m_headerPanel.clear();
		m_textArea.clear();
	}

	public void appendText(List<String> lines) {
		m_textArea.append(lines);
	}

	public void setText(List<String> lines) {
		m_textArea.setText(lines);
	}

	public void applyParticipants(List<ClientParticipant> participantList) {
		if (m_openWaveId == null) {
			// nothing to do;
			return;
		}

		for (ClientParticipant p : participantList) {

			if (m_openWaveId.equals(p.getWaveId())) {
				if (p.isAdd()) {
					m_headerPanel.addParticipant(p.getParticipantId());
				} else {
					m_headerPanel.removeParticipant(p.getParticipantId());
				}
			}
		}
	}

	public void setOpenWave(ClientWaveId openWaveId) {
		m_openWaveId = openWaveId;
		m_headerPanel.setWave(openWaveId);
	}

	public void applyData(ClientCommand clientCommand) {
		if (clientCommand.getMessageData() == null) {
			return;
		}

		if (clientCommand.isMessageDataDelta()) {
			appendText(clientCommand.getMessageData());
		} else {
			setText(clientCommand.getMessageData());
		}
		// m_tWavecontent.append("Wave: "
		// + clientCommand.getWaveId().getId() + "\n"
		// + buf.toString());
	}
}
